import os
import requests
import re


def get_reqponse(html_url):
    headers = {
        'referer': 'https://www.dongmanmanhua.cn/',
        'User-Agent': 'Mozilla//5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response

def get_info(html_url):
    html_data = get_reqponse(html_url).text
    name = re.findall("title_title: '(.*?)',", html_data)[0]  # 提取漫画名字
    chapter_url_list = re.findall('data-sc-name="PC_detail-page_related-title-list-item".*?href="(.*?)"', html_data, re.S)
    title_list = re.findall('<span class="subj"><span>(.*?)</span></span>', html_data)
    return name, chapter_url_list, title_list

def get_img_url(chapter_url):
    chapter_data = get_reqponse(chapter_url).text
    img_url_list = re.findall('alt="image" class="_images _centerImg" data-url="(.*?)"', chapter_data)
    return img_url_list

def save_img(name, title, img_url):
    file = f'img/{name}/'
    if not os.path.exists(file):
        os.makedirs(file)
    img_content = get_reqponse(img_url).content
    with open(file + title, 'wb') as f:
        f.write(img_content)
    print(name, title)


if __name__ == '__main__':
   for page in range(12, 0, -1):
       
       link=f'https://www.dongmanmanhua.cn/BOY/moutianchengweimoshen/list?title_no=1519&page={page}'
       response = get_info(link)
       name, chapter_url_list, title_list = response
    #    print(name)
    #    print(chapter_url_list)
    #    print(title_list)

       for chapter_url, chapter_title in zip(chapter_url_list, title_list):
        chapter_url = 'https:' + chapter_url
        img_url_list = get_img_url(chapter_url)
        num =1
        for img_url in img_url_list:
            save_img(name, chapter_title+str(num)+".jpg" , img_url)
            num+=1
            print(img_url)
      

   